<?php

namespace app\api\controller\agent;


use app\admin\model\Shopcharge;
use app\admin\model\Shoporder;
use app\admin\model\Shopwifi;
use app\admin\model\Userorderprofit;
use app\common\controller\Api;

/**
 *
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index(){

        $user = $this->auth->getUser();
        $date = $this->request->param('type');


        $shopModel = new \app\admin\model\Shop();
        $userOrderProfitModel = new Userorderprofit();
        $shopOrderModel = new Shoporder();

        $ret = array();



        $ids = array();
        $list = $shopModel->where('agent_id',$user['id'])->select();
        foreach ($list as $key=>$val){
            $ids[] = $val['id'];
        }

        if($date == 1){
            $profit = $userOrderProfitModel->whereTime('create_time','today')->where('user_id',$user['id'])->sum('amount');
            $shopCount = $shopModel->where('agent_id',$user['id'])->count('id');
            $totalAmount = $shopOrderModel->whereTime('create_time','today')->where('shop_id','in',$ids)->sum('pay_amount');
            $totalCount = $shopOrderModel->whereTime('create_time','today')->where('shop_id','in',$ids)->count('id');

        }else if($date == 2){

            $profit = $userOrderProfitModel->whereTime('create_time','yesterday')->where('user_id',$user['id'])->sum('amount');
            error_log("===yes=====".$profit);
            $shopCount = $shopModel->where('agent_id',$user['id'])->count('id');
            $totalAmount = $shopOrderModel->whereTime('create_time','yesterday')->where('shop_id','in',$ids)->sum('pay_amount');
            $totalCount = $shopOrderModel->whereTime('create_time','yesterday')->where('shop_id','in',$ids)->count('id');

        }else{
            $profit = $userOrderProfitModel->whereTime('create_time','month')->where('user_id',$user['id'])->sum('amount');
            $shopCount = $shopModel->where('agent_id',$user['id'])->count('id');
            $totalAmount = $shopOrderModel->whereTime('create_time','month')->where('shop_id','in',$ids)->sum('pay_amount');
            $totalCount = $shopOrderModel->whereTime('create_time','month')->where('shop_id','in',$ids)->count('id');

        }

        $ret['profit'] = $profit;
        $ret['shop'] = $shopCount;
        $ret['totalAmount'] = $totalAmount;
        $ret['totalOrder'] = $totalCount;

        $this->success($user['id'],$ret);
    }

    





}
